﻿using System;
using System.Collections.Generic;
using System.Linq;
using GdHotel.Data.Interfaces;

namespace GdHotel.Data.Repositories
{
    public class GalleriesRepository : IGalleriesRepository
    {
        private GdHotelEntities entities = new GdHotelEntities();

        public IQueryable<Gallery> FindAll()
        {
            return entities.Galleries.AsQueryable();
        }

        public IQueryable<Gallery> FindAllByLang(Language lang)
        {
            throw new NotSupportedException("This operation does not supported.");
        }

        public Gallery GetById(Int32 id)
        {
            return entities.Galleries.SingleOrDefault(gallery => gallery.Id == id);
        }

        public void Insert(Gallery gallery)
        {
            entities.AddToGalleries(gallery);
        }

        public void Delete(Gallery gallery)
        {
            entities.Galleries.DeleteObject(gallery);
        }

        public void Save()
        {
            entities.SaveChanges();
        }

        public IEnumerable<String> GetLinksAsArray(Gallery gallery)
        {
            return gallery.Links.Split(new[] { ',', ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public IQueryable<Gallery> FindAllByKey(String key)
        {
            return entities.Galleries
                .Where(g => g.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
                .AsQueryable();
        }

        public void Dispose()
        {
            entities.Dispose();
        }
    }
}
